package com.sky.SpringTask;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class MyTask {

    @Autowired
    OrderMapper orderMapper;
    @Scheduled(cron = "0 * * * * ? ")
    public void processOrderTimeOut(){
        List<Orders> kks = orderMapper.getListTimeOut(Orders.PENDING_PAYMENT, LocalDateTime.now().plusMinutes(-15));
        for (int i = 0; i < kks.size(); i++) {
            kks.get(i).setStatus(Orders.CANCELLED);
            kks.get(i).setCancelReason("订单超时");
            kks.get(i).setCancelTime(LocalDateTime.now());
            orderMapper.update(kks.get(i));
        }
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void PaiSongING(){
        List<Orders> kks = orderMapper.getListTimeOut(Orders.DELIVERY_IN_PROGRESS,LocalDateTime.now().plusHours(-1));
        for (int i = 0; i < kks.size(); i++) {
            kks.get(i).setStatus(Orders.COMPLETED);
            orderMapper.update(kks.get(i));

        }
    }
}
